﻿using UnityEngine;
using System.Collections;

public class MultiplayerScript : MonoBehaviour{
	
	public string titleMessage = "Project Element Prototype";
	private string connectToIP = "127.0.0.1";
	private int connectionPort;
	private bool useNAT = false;
	private string ipAddress;
	private string port;
	private int numberOfPlayers = 6;
	public string playerName;
	public string serverName;
	public string serverNameForClient;
	private bool iWantToSetupAServer = false;
	private bool iWantToConnectToAServer = false;
	
	//viariáveis para a GUI principal
	private Rect connectionWindowRect;
	private int connectionWindowWidth = 400;
	private int connectionWindowHeight = 200;
	private int buttonHeight = 60;
	private int leftIndent;
	private int topIndend;
	
	//variáveis para a GUI de desligar o servidor
	private Rect serverDisWindowRect;
	private int serverDisWindowWidth = 300;
	private int serverDisWindowHeight = 150;
	private int serverDisWindowLeftIndent = 10;
	private int serverDisWindowTopIndent = 10;
	
	public enum GameState{
		MainMenu,
		Playing
	}
	public enum MainMenuState{
		ParentMenu,
		CreateServer,
		JoinServer
	}
	
	public GameState gameState;
	public MainMenuState mainMenuState;
	
	// Use this for initialization
	void Start(){
		serverName = PlayerPrefs.GetString("serverName");
		if(serverName == "")
			serverName = "Servidor";
		connectionPort = PlayerPrefs.GetInt("connectionPort");
		if(connectionPort == 0){
			connectionPort = 26500;
		}
		playerName = PlayerPrefs.GetString("playerName");
		if(playerName == "")
			playerName = "Jogador";
		gameState = GameState.MainMenu;
	}
	
	void ConnectWindow(int windowID){
		//deixar um espaço no inicio
		GUILayout.Space(15);
		//quando o jogador inicia o jogo ele pode criar
		//um jogo ou entrar em um jogo
		switch(mainMenuState){
		case MainMenuState.ParentMenu:
			if(GUILayout.Button("Criar Servidor", GUILayout.Height(buttonHeight))){
				mainMenuState = MainMenuState.CreateServer;
			}
			GUILayout.Space(10);
			if(GUILayout.Button("Conectar a um servidor", GUILayout.Height(buttonHeight))){
				mainMenuState = MainMenuState.JoinServer;
			}
			GUILayout.Space(10);
			if(GUILayout.Button("Sair", GUILayout.Height(buttonHeight))){
				Application.Quit();
			}
			break;
			
		//jogador quer ser o host de uma sala
		case MainMenuState.CreateServer:
			GUILayout.Label("Nome do Servidor:");
			serverName = GUILayout.TextField(serverName);
			GUILayout.Space(5);
			GUILayout.Label("Nome do Jogador:"); //TODO: trocar pelo nome no BD
			playerName = GUILayout.TextField(playerName);
			GUILayout.Space(5);
			GUILayout.Label("Porta do Servidor: (Padrão 26500)");
			connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString(), 5));
			GUILayout.Space(5);
			if(GUILayout.Button("Iniciar servidor", GUILayout.Height(25))){
				if(serverName == ""){
					serverName = "Servidor";
				}
				Network.InitializeServer(numberOfPlayers, connectionPort, useNAT);
				PlayerPrefs.SetString("serverName", serverName);
				PlayerPrefs.SetInt("connectionPort", connectionPort);
				PlayerPrefs.SetString("playerName", playerName);
				gameState = GameState.Playing;
				//TODO: adicionar instanciamento do jogador
			}
			GUILayout.Space(5);
			if(GUILayout.Button("Voltar", GUILayout.Height(25))){
				mainMenuState = MainMenuState.ParentMenu;
			}
			break;
			
		//jogador quer se conectar a um servidor
		case MainMenuState.JoinServer:
			GUILayout.Label("Nome do Jogador:"); //TODO: trocar pelo nome no BD
			playerName = GUILayout.TextField(playerName);
			GUILayout.Space(5);
			GUILayout.Label("IP do Servidor:");
			connectToIP = GUILayout.TextField(connectToIP);
			GUILayout.Space(5);
			GUILayout.Label("Porta do Servidor:");
			connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString(), 5));
			GUILayout.Space(5);
			if(GUILayout.Button("Conectar", GUILayout.Height(25))){
				if(playerName == "")
					playerName = "Jogador";
				Network.Connect(connectToIP, connectionPort);
				PlayerPrefs.SetString("playerName", playerName);
				PlayerPrefs.SetInt("connectionPort", connectionPort);
				PlayerPrefs.SetString("serverName", serverName);
				gameState = GameState.Playing;
			}
			GUILayout.Space(5);
			if(GUILayout.Button("Voltar", GUILayout.Height(25))){
				mainMenuState = MainMenuState.ParentMenu;
			}
			break;
		}
	}//connectWindow
	
	void CloseServerWindow(int windowID){
		GUILayout.Label("Nome do servidor: " + serverName);
		GUILayout.Label("Número de jogadores online: " + Network.connections.Length);
		if(Network.connections.Length >= 1){
			GUILayout.Label("Ping: " + Network.GetAveragePing(Network.connections[0]));
		}
		if(GUILayout.Button("Finalizar Servidor")){
			Network.Disconnect();
			gameState = GameState.MainMenu;
		}
	}
	
	void OnGUI(){
		//exibir a tela de conexao quando o jogador estiver desconectado
		switch(gameState){
		case GameState.MainMenu:
			//determinar a posiçao da tela baseado no tamanho do monitor
			//a janela ficará centralizada
			leftIndent = Screen.width / 2 - connectionWindowWidth / 2;
			topIndend = Screen.height / 2 - connectionWindowHeight / 2;
			connectionWindowRect = new Rect(leftIndent, topIndend, 
			                                connectionWindowWidth, connectionWindowHeight);
			connectionWindowRect = GUILayout.Window(0, connectionWindowRect, ConnectWindow,
			                                        titleMessage);
			break;
		case GameState.Playing:
			if(Network.peerType == NetworkPeerType.Server){
				serverDisWindowRect = new Rect(serverDisWindowLeftIndent, serverDisWindowTopIndent,
				                               serverDisWindowWidth, serverDisWindowHeight);
				serverDisWindowRect = GUILayout.Window(1, serverDisWindowRect, CloseServerWindow, "");
			}
			break;
		}
	}
	
}




























